package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Client;
import cn.wolfcode.crm.domain.ClientLost;
import cn.wolfcode.crm.domain.StudentOfficial;
import cn.wolfcode.crm.query.StudentOfficialQueryObject;
import cn.wolfcode.crm.service.IStudentOfficialService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PageResult;
import com.alibaba.druid.support.json.JSONUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.List;


@Controller
@RequestMapping("studentofficial")
public class StudentOfficialController {
    @Autowired
    private IStudentOfficialService studentOfficialService;

    @RequiresPermissions(value = {"studentofficial:view", "正式学员管理查看"}, logical = Logical.OR)
    @RequestMapping("view")
    public String view() {
        return "studentofficial/studentofficial";
    }

    //做学生查询回显
    @RequestMapping("getOwnTuitionByClientId")
    @ResponseBody
    public StudentOfficial getOwnTuitionByClientId(Long id) {
        StudentOfficial ownTuitionByClientId = studentOfficialService.getOwnTuitionByClientId(id);
        return studentOfficialService.getOwnTuitionByClientId(id);
    }

    @RequiresPermissions(value = {"studentofficial:query", "正式学员管理列表"}, logical = Logical.OR)
    @RequestMapping("query")
    @ResponseBody
    public PageResult query(StudentOfficialQueryObject qo) {
        return studentOfficialService.query(qo);
    }

    @RequiresPermissions(value = {"studentofficial:saveOrUpdate", "正式学员管理新增/编辑"}, logical = Logical.OR)
    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(StudentOfficial studentOfficial){
        JsonResult result = new JsonResult();
        try{
             studentOfficialService.saveOrUpdate(studentOfficial);
        }catch (Exception e){
            result.mark(e.getMessage());
            return result;
        }
        return result;
    }

    @RequiresPermissions(value = {"studentofficial:selectByPrimaryKey", "正式学员管理查看单条数据"}, logical = Logical.OR)
    @RequestMapping("selectByPrimaryKey")
    @ResponseBody
    public StudentOfficial selectByPrimaryKey(Long id){
        return studentOfficialService.selectByPrimaryKey(id);
    }


    @RequestMapping("change2quit")
    @ResponseBody
    public JsonResult change2quit(Long id){
        JsonResult result = new JsonResult();
        try{
            studentOfficialService.change2quit(id);
        }catch (Exception e){
            result.mark(e.getMessage());
            return result;
        }
        return result;
    }

    @RequestMapping("change2class")
    @ResponseBody
    public JsonResult change2class(Long id,Long newclassId){
        System.out.println(newclassId);
        System.out.println(id);
        JsonResult result = new JsonResult();
        try{
            studentOfficialService.change2class(id,newclassId);
        }catch (Exception e){
            result.mark(e.getMessage());
            return result;
        }
        return result;
    }

    @RequestMapping("change2lost")
    @ResponseBody
    public JsonResult change2lost(ClientLost cl) {
        JsonResult result = new JsonResult();
        try {
            studentOfficialService.change2lost(cl);
        } catch (Exception e) {
            result.mark(e.getMessage());
            return result;
        }
        return result;
    }


  /*  @RequiresPermissions(value = {"studentOfficial:saveOrUpdate", "正式学员管理新增/编辑"}, logical = Logical.OR)
    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public JsonRest saveOrUpdate(StudentOfficial entity) {
        try {
            studentOfficialService.saveOrUpdate(entity);
            return new JsonRest();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonRest().mark("操作失败!原因:" + e.getMessage());
        }
    }*/

   @RequestMapping("exportXls")
    @ResponseBody
    public void exportXls(HttpServletResponse response, StudentOfficialQueryObject qo) throws Exception {
        //设置文件下载响应头

        response.setHeader("Content-Disposition", "attachment;filename=studentOfficial.xls");

        Workbook book = studentOfficialService.exportXls(qo);

        //写入数据输出到浏览器
        book.write(response.getOutputStream());
    }

    @RequiresPermissions(value = {"studentofficial:sengMsg", "催交学费"}, logical = Logical.OR)
    @RequestMapping("sengMsg")
    @ResponseBody
    public JsonResult sengMsg(StudentOfficial studentOfficial){
        JsonResult result = new JsonResult();
        try{
            int i = studentOfficialService.sengMsg(studentOfficial);
            if(i != 200){
                result.mark("发送错误,错误代码："+i);
            }
        }catch (Exception e){
            result.mark(e.getMessage());
            return result;
        }
        return result;
    }
}
